{% extends "base_list.html" %}

{% block more_filter -%}
<div class="form-group">
    <label class="col-sm-2 control-label">上线</label>
    {{ list_radio("is_online") }}
</div>
{%- endblock more_filter %}
{% block searchbar %}
{% endblock searchbar %}
{% block list_body %}
    <div class="row">
        <div class="col-lg-12">
            <div class="panel panel-default">
                <div class="panel-body">
                    <div class="row mbh">

                    <div class="col-sm-12 dataTables_filter">
                        <a href="{{ url('institution:admin_create', kwargs={'institution_id': institution_id}) }}" class="btn btn-primary">
                            <span class="glyphicon glyphicon-plus" aria-hidden="true"></span>
                            新增
                        </a>
                    </div>

                    </div>

                    <div class="table-responsive">
                        <table class="table table-striped table-bordered table-hover" id="dataTables">
                            <thead>
                            <tr>
                                <th>ID</th>
                                <th>管理员类型</th>
                                <th>部门</th>
                                <th>用户名</th>
                                <th>角色</th>
                                <th>手机号</th>
                                <th>创建时间</th>
                                <th>状态</th>
                                <th>操作</th>
                            </tr>
                            </thead>
                            <tbody id="id-list">
                            </tbody>
                        </table>
                    </div>
                    <!-- /.dataTable_wrapper -->
                </div>
                <!-- /.panel-body -->
            </div>
            <!-- /.panel -->
        </div>
    </div> <!-- /.row -->
{% endblock list_body %}

{% block body_js %}
<script type="text/javascript">
    function reset_status(id) {
        bootbox.confirm({
            size: "small",
            title: "冻结状态",
            message: "确认更改",
            callback: function (result) {
                if(result){
                    $.ajax({
                        type: "POST",
                        url: "/accounts/admin/reset_status/",
                        data: {
                            "id": id,
                            "csrfmiddlewaretoken": "{{ csrf_token }}",
                        },
                        dataType: "json",
                        success: function (data) {
                            search();
                        },
                    });
                }
            }
        });
    }
    function reset_password(id){
        var pwd = '';
        bootbox.prompt({
            size: "small",
            title: "请输入密码",
            callback: function(result){
                pwd = result;
                if (result === null) {
                    return;
                } else {
                    pwd = result;
                    $.ajax({
                        type: "POST",
                        url: "/accounts/admin/reset_password/",
                        data: {
                            "id": id,
                            "pwd": pwd,
                            "csrfmiddlewaretoken": "{{ csrf_token }}",
                        },
                        dataType: "json",
                        success: function (data) {
                            search();
                        },
                    });
                }
            }/* result = String containing user input if OK clicked or null if Cancel clicked */
        });

    }
$(document).ready(function () {
    var table = $('#dataTables').dataTable({
        "dom": 'ltpr',
        "language": {
            "url": "{{ static('lib/js/dataTable.Chinese.json') }}"
        },
        "responsive": true,
        "lengthChange": false,
        "serverSide": true,
        "ajax": {
            url: "/institution/admin_datatable/",
            type: "POST",
            data: {
                    csrfmiddlewaretoken: "{{ csrf_token }}",
                    institution_id: "{{ institution_id }}",
                },
        },
        "columns": {{ list_fields.to_json()|safe }},
        "searchCols": [
            {% for col in list_fields.to_list() -%}
            {% if col.name in searches -%}
            {{ searches[col.name]|jsonify|safe }},
            {% else %}
            null,
            {%- endif %}
            {%- endfor %}
        ],
        "columnDefs": [
            {
                "targets": [8],
                 "render": function ( data, type, full, meta ) {
                    var s = full.status;
                    var status_show = '冻结';
                    if (s=='已冻结'){
                        status_show = '解冻';
                    }
                    console.log(full);
                     return '<a href="/institution/institution_list/admin_list/admin_modify/{{ institution_id }}/'+data+'">编辑</a>&nbsp;' +
                         '<a href="#" onclick="reset_password('+data+')">重置密码</a>&nbsp;' +
                         '<a href="#" onclick="reset_status('+data+')">'+ status_show+'</a>&nbsp;';
                },
            },
        ],
        "order": [[0, 'desc']],
        "processing": true,
        "deferRender": true,
        {#
        {% if not searches -%}
        "deferLoading": true,
        {%- endif %}
        #}
        "pageLength": 10,
    });


    $('#function').change(function(){
        var $selAll = $('#select-allpage');
        if($(this).val()=='set_refunded'){
            $selAll.removeAttr('checked');
            $selAll.attr('disabled','disabled');
        }else{
            $('#select-allpage').removeAttr('disabled');
        }
    });
    //=== search click ===
    $("#id-search-btn").on("keyup click", function () {
        search();
    });

    $("#id-th-checkbox").on("change", function () {
        $("#id-list").find("input[type=checkbox]").prop("checked", $(this).prop("checked"));
    });


    $("#select-allpage").on("click", function () {
        $("#id-th-checkbox").prop("checked", $("#select-allpage").prop("checked"));
        $("#id-list").find("input[type=checkbox]").prop("checked", $("#select-allpage").prop("checked"));
    });

});
</script>


{% endblock %}
